﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Jixiao
{
    class MonthPinItem
    {
        public string UserName { get; set; }
        public int Score { get; set; }
        public string ProjectName { get; set; }
        public string Reason { get; set; }
        public string YearMonth { get; set; }

        public static List<MonthPinItem> GetItems(string yearMonth)
        {
            var con = Form1.Connection;
            var dt = con.SelectData("select * from MonthPin where YearMonth='" + yearMonth + "' order by UserName");

            var pinItems = new List<MonthPinItem>();
            foreach (DataRow row in dt.Rows)
            {
                var pinItem = new MonthPinItem
                {
                    UserName = row["UserName"].ToString(),
                    Score = Convert.ToInt32(row["Score"]),
                    ProjectName = row["ProjectName"].ToString(),
                    Reason = row["Reason"].ToString(),
                    YearMonth = yearMonth
                };
                pinItems.Add(pinItem);
            }

            return pinItems;
        }

        internal static string GetComments(List<MonthPinItem> m_monthPinItems, string projectName, string m_lastUserName)
        {
            for (var i = 0; i < m_monthPinItems.Count; i++)
            {
                if (m_monthPinItems[i].UserName == m_lastUserName )
                {
                    if (string.IsNullOrEmpty(projectName) == false && m_monthPinItems[i].ProjectName != projectName)
                        continue;
                    return m_monthPinItems[i].Reason;
                }
            }
            return "";
        }

        internal static string GetScore(List<MonthPinItem> m_monthPinItems, string projectName, string m_lastUserName, int qz)
        {
            var q = GetScoreRadio(m_monthPinItems, projectName, m_lastUserName);
            return ((int)(q * qz / 100)).ToString();
        }

        internal static int GetScoreRadio(List<MonthPinItem> m_monthPinItems, string projectName, string m_lastUserName)
        {
            for (var i = 0; i < m_monthPinItems.Count; i++)
            {
                if (m_monthPinItems[i].UserName == m_lastUserName && m_monthPinItems[i].ProjectName == projectName)
                {
                    return m_monthPinItems[i].Score;
                }
            }
            return 100;
        }
    }
}
